
from ctrader_open_api import Client, Protobuf, TcpProtocol, Auth, EndPoints
from ctrader_open_api.messages.OpenApiCommonMessages_pb2 import *
from ctrader_open_api.messages.OpenApiMessages_pb2 import *
from ctrader_open_api.messages.OpenApiModelMessages_pb2 import *
from twisted.internet import reactor
import json
import datetime
import calendar

def get_xauusd_ochl(timeframe='D1', limit=100):
    """
    获取XAUUSD的OCHL数据
    Args:
        timeframe: 时间周期 (M1, M5, M15, H1, H4, D1, W1, MN1)
        limit: 获取的数据条数
    Returns:
        list of dict: [{
            'open': float,
            'close': float, 
            'high': float,
            'low': float,
            'time': datetime
        }]
    """
    client = Client(host=)
    client.connect()
    
    try:
        # 获取XAUUSD的K线数据
        symbol = "XAUUSD"
        candles = client.get_candles(symbol, timeframe, limit)
        
        ochl_data = []
        for candle in candles:
            ochl_data.append({
                'open': candle.open,
                'close': candle.close,
                'high': candle.high,
                'low': candle.low,
                'time': candle.time
            })
            
        return ochl_data
        
    finally:
        client.disconnect()

if __name__ == '__main__':
    # 示例用法
    data = get_xauusd_ochl()
    for i, candle in enumerate(data[:5]):  # 打印前5条数据
        print(f"Candle {i+1}: {candle}")
